package client;

import api.*;
import metier.*;
import snippet.tickerHandler;

import java.util.Date;
import java.util.Observable;

/**
 * Created with IntelliJ IDEA.
 * User: keita
 * Date: 05/06/13
 * Time: 23:19
 * To change this template use File | Settings | File Templates.
 */

public class Mywraper  implements EWrapper {
    private static Mywraper instance;
    private tickerHandler handler;


   synchronized public static Mywraper getInstance() {
        if (null == instance) { // Premier appel
            instance = new Mywraper();

        }
        return instance;
    }

    public Mywraper() {
        handler = new tickerHandler();

    }

    @Override // on EWrapper interface, in callback thread
    public void contractDetails(int reqId, ContractDetails contractDetails) {
        contractInfo conteneur = new contractInfo(reqId,contractDetails);
        handler.send(conteneur);

//        System.out.println("contractDetails:"+EWrapperMsgGenerator.contractDetails(reqId,contractDetails));
    }



    @Override
    public void tickPrice(int tickerId, int field, double price, int canAutoExecute) {

        tickPrice conteneur = new tickPrice(tickerId, field, price, canAutoExecute);
//        handler.send(conteneur);
       System.out.println(EWrapperMsgGenerator.tickPrice(tickerId,field,price,canAutoExecute));;
    }

    @Override
    public void tickSize(int tickerId, int field, int size) {
        tickSize conteneur = new tickSize(tickerId, field , size);
//        handler.send(conteneur);
        System.out.println(EWrapperMsgGenerator.tickSize(tickerId,field,size));



    }

    @Override
    public void tickOptionComputation(int tickerId, int field, double impliedVol, double delta, double optPrice, double pvDividend, double gamma, double vega, double theta, double undPrice) {
    }

    @Override
    public void tickGeneric(int tickerId, int tickType, double value) {
        System.out.println("ti");
    }

    @Override
    public void tickString(int tickerId, int tickType, String value) {

        System.out.println("tickString: " +EWrapperMsgGenerator.tickString(tickerId,tickType,value));

    }

    @Override
    public void tickEFP(int tickerId, int tickType, double basisPoints, String formattedBasisPoints, double impliedFuture, int holdDays, String futureExpiry, double dividendImpact, double dividendsToExpiry) {
    }

    @Override
    public void orderStatus(int orderId, String status, int filled, int remaining, double avgFillPrice, int permId, int parentId, double lastFillPrice, int clientId, String whyHeld) {
    }

    @Override
    public void openOrder(int orderId, Contract contract, Order order, OrderState orderState) {
    }

    @Override
    public void openOrderEnd() {
    }

    @Override
    public void updateAccountValue(String key, String value, String currency, String accountName) {
        System.out.print("updateAccountValue :  "+value);
        System.out.println(currency +" "+accountName);
    }

    @Override
    public void updatePortfolio(Contract contract, int position, double marketPrice, double marketValue, double averageCost, double unrealizedPNL, double realizedPNL, String accountName) {
        System.out.print("updatePortfolio "+contract.m_symbol);
        System.out.print(position+" "+marketPrice+" ");
        System.out.println(unrealizedPNL+" "+realizedPNL+" ");
    }

    @Override
    public void updateAccountTime(String timeStamp) {
        System.out.println(" heure : "+timeStamp);
    }

    @Override
    public void accountDownloadEnd(String accountName) {
    }

    @Override
    public void nextValidId(int orderId) {
    }


    @Override
    public void bondContractDetails(int reqId, ContractDetails contractDetails) {

        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void contractDetailsEnd(int reqId) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
    @Override
    public void execDetails(int reqId, Contract contract, Execution execution) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void execDetailsEnd(int reqId) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void updateMktDepth(int tickerId, int position, int operation, int side, double price, int size) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void updateMktDepthL2(int tickerId, int position, String marketMaker, int operation, int side, double price, int size) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void updateNewsBulletin(int msgId, int msgType, String message, String origExchange) {
        System.out.println(msgType);
        System.out.println(message);
        System.out.println(origExchange);
    }

    @Override
    public void managedAccounts(String accountsList) {
        System.out.println("managedAccounts: "+accountsList);
    }

    @Override
    public void receiveFA(int faDataType, String xml) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void historicalData(int reqId, String date, double open, double high, double low, double close, int volume, int count, double WAP, boolean hasGaps) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void scannerParameters(String xml) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void scannerData(int reqId, int rank, ContractDetails contractDetails, String distance, String benchmark, String projection, String legsStr) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void scannerDataEnd(int reqId) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void realtimeBar(int reqId, long time, double open, double high, double low, double close, long volume, double wap, int count) {
        System.out.println(open);
    }

    @Override
    public void currentTime(long time) {
        System.out.println(new Date(time));
    }

    @Override
    public void fundamentalData(int reqId, String data) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void deltaNeutralValidation(int reqId, UnderComp underComp) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void tickSnapshotEnd(int reqId) {
        System.out.println("snapprea");
    }


    @Override
    public void marketDataType(int reqId, int marketDataType) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void commissionReport(CommissionReport commissionReport) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void error(Exception e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void error(String str) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void error(int id, int errorCode, String errorMsg) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void connectionClosed() {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
